const Ws = require('ws')
const express = require('express')
const compression = require('compression')

const app = express()


// 一定要把这行代码，写到，静态资源托管之前
app.use(compression())
app.use(express.static('./dist'))

app.listen(8080, () => {
    console.log('server running at http://127.0.0.1:8080')
})

;
(Ws => {
    const server = new Ws.Server({
        port: 8000
    })

    const init = () => {
        bindEvent()
    }


    const bindEvent = () => {
        server.on('open', () => {
            console.log('服务打开');
        })
        server.on('close', () => {
            console.log('服务关闭');
        })
        server.on('error', () => {
            console.log('服务错误');
        })
        server.on('connection', ws => {
            ws.on('message', handleMsg)
        })
    }

    function handleMsg(msg) {
        server.clients.forEach(c => {
            c.send(msg)
        })
    }

    init()
})(Ws)